<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\material\base;

use app\enum\CommonEnum;
use app\model\mysql\StaffRelationModel;
use app\model\mysql\StaffRoleModel;
use app\model\ZcSubtasksMemberModel;

trait StaffTrait
{
    /**
     * 获取素材创意人员.
     *
     * @param int $subtaskId 子任务id
     */
    protected static function getCreativeStaff(int $subtaskId = 0): array
    {
        if (! $subtaskId) {
            return [];
        }
        return (new ZcSubtasksMemberModel())
            ->where('subtask_id', $subtaskId)
            ->column('user_id');
    }

    /**
     * 获取素材创意设计.
     *
     * @param int $subtaskId 子任务id
     */
    protected static function getCreativeRoleStaff(int $subtaskId = 0)
    {
        if (! $subtaskId) {
            return [];
        }
        return (new ZcSubtasksMemberModel())->alias('zsm')
            ->join(['habo_workflow_step' => 'ws'], 'zsm.workflow_id=ws.workflow_id and zsm.workflow_step_id=ws.step_position')
            ->where('zsm.subtask_id', $subtaskId)
            ->field('user_id,ws.step_role as role_id')
            ->select();
    }

    /**
     * 获取素材创意导演.
     *
     * @param int $subtaskId 子任务id
     */
    protected static function getCreativeDirectStaff(int $subtaskId = 0)
    {
        if (! $subtaskId) {
            return [];
        }
        return (new ZcSubtasksMemberModel())
            ->where('subtask_id', $subtaskId)
            ->where('direct_id > 0')
            ->value('direct_id');
    }

    /**
     * 获取素材创意制片.
     * @param int $teamId 团队id
     * @param int $departmentId 部门id
     */
    protected static function getCreativeProductionStaff(int $teamId = 0, int $departmentId = 0)
    {
        if (! $departmentId) {
            return [];
        }
        $query = (new StaffRelationModel())
            ->alias('sr')
            ->join([(new StaffRoleModel())->getTable() => 'r'], 'sr.staff_id=r.staff_id')
            ->where('r.role_id', CommonEnum::ROLE_PRODUCTION)
            ->where('sr.is_department_master', 1)
            ->where('sr.department_id', $departmentId);
        if ($teamId) {
            $query->whereOr([['sr.team_id', '=', 0], ['sr.team_id', '=', $teamId]]);
        } else {
            $query->where('sr.team_id', 0);
        }

        return $query->value('sr.staff_id');
    }

    /**
     * 获取素材创意组负责人.
     *
     * @param int $teamId 团队id
     */
    protected static function getCreativeTeamStaff(int $teamId)
    {
        if (! $teamId) {
            return [];
        }
        return (new StaffRelationModel())
            ->where('team_id', $teamId)
            ->where('is_team_main', 1)
            ->where('is_department_master', 1)
            ->value('staff_id');
    }

    /**
     * 获取素材创意部门负责人.
     *
     * @param int $departmentId 部门id
     */
    protected static function getCreativeDeptStaff(int $departmentId = 0)
    {
        if (! $departmentId) {
            return [];
        }
        return (new StaffRelationModel())
            ->where('department_id', $departmentId)
            ->where('is_department_main', 1)
            ->where('is_department_master', 1)
            ->value('staff_id');
    }
}
